前一阵子一直研究Linux系统如何备份恢复。在一篇中文博客中发现的一种简单方法,那就使用tar命令。由于事关重大,搞不好自己的服务器会挂掉,所以一直没有尝试,后来不了了之。
最近想把树莓派的系统盘由usb换回sd,于是又查到了Ubuntu官方的文档,涨了不少知识,现在记录一下。先上备份的命令。
tar -cvpzf /var/hdd500g/backup.tar.gz --exclude=/var/hdd500g/* --one-file-system /
解释一下:
-c 新建压缩文件
-v 显示模式,压缩时会显示每一个处理文件。(就看屏幕刷刷的。。。)
-p 保存文件权限信息
-z gzip压缩方法,也可以换成bz2的j
-f 指定备份文件位置
–exclude 跳过不需要备份的文件(可以用*指定),可以多重指定(不能光指定路径,否则里面的文件会被备份。。。)
最重要的,是下面这个。
–one-file-system 简而言之,就是只备份必要的文件。忽略内存里创建的,还有挂载的。官方给出的英文解释如下:
Do not include files on a different filesystem. If you want other filesystems, such as a /home partition, or external media mounted in /media backed up, you either need to back them up separately, or omit this flag. If you do omit this flag, you will need to add several more –exclude= arguments to avoid filesystems you do not want. These would be /proc, /sys, /mnt, /media, /run and /dev directories in root. /proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. /dev is a tmpfs whose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it. Likewise, /run is a tmpfs that holds variables about the running system that do not need backed up.
最后这个真是太有帮助了,学到了感觉赚到了,哈哈
最后一个“/”,要备份的目录。千万不要一高兴忘记敲啦!
恢复系统命令如下。
tar -xvpzf /home/ubuntu/backup.tar.gz -C /
-x 解压缩
-C 解压缩位置,根目录,别弄错啦。否则就不是恢复系统啦!!!
我是在两个相同版本的Ubuntu下敲的命令,重启一下,一切正常!
祝你好运!
----------------
参考文章: